/**
 * Created by luiz.alberto on 11/11/2014.
 */
function createWaitModal(){
    var waitModal = $('<div class="modal static" id="pleaseWaitDialog" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog modal-sm"><div class="modal-content"><div class="modal-header"><h4>Aguarde...</h4></div><div class="modal-body"><div class="progress progress-striped active"><div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%">Carregando...</div></div></div></div></div></div>');
    var dialogEx = $.extend(waitModal, {
        show: function() { waitModal.modal({ backdrop: 'static', keyboard: false }); },
        hide: function(){ waitModal.modal('hide'); }
    });
    return dialogEx;
}

function createModal(fnExecute, size){
    var dialog  = $('<div class="modal fade" id="modalComponent" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h3 class="modal-title text-success"></h3></div><div class="modal-body"></div><div class="modal-footer"><button type="button" class="btn btn-success btn-sim"><span class="glyphicon glyphicon-ok-circle"></span> <span class="confirm_button">Sim</span></button><button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove-circle"></span> <span class="negative_button">Não</span></button></div></div></div></div>').appendTo('body');
    dialog.find('.modal-dialog').addClass("modal-"+size);
    dialog.addClass("bs-example-modal-"+size);
    
    var dialogEx = $.extend(dialog, {
        setTitle: function(title) { dialog.find('.modal-title').text(title); return dialog;},
        setMessage: function(message) { dialog.find('.modal-body').text(message); return dialog;},
        setContent: function(content) {
            //dialog.find('.modal-title').text(content.find('legend').text());
            //content.find('legend').remove();
            dialog.find('.modal-body').html(content);
            return dialog;
        },
        setConfirmText: function(text) { dialog.find('.confirm_button').text(text); return dialog;},
        setNegativeText: function(text) { dialog.find('.negative_button').text(text); return dialog;},
        hideConfirmButton: function(hide) { dialog.find('.btn-sim').hide(); return dialog;},
        show: function() { dialog.modal('show'); return dialog;},
        hide: function(){ dialog.modal('hide'); return dialog;}
    });
    
    dialog.find('.btn-sim').click(function(){fnExecute(dialog);});
    return dialogEx;
};

$.fn.ShowAsModal = function(obj){
	var component = $(this);
	var modal = createModal(obj.execute, obj.size);
	modal.setContent(component);
	modal.setTitle(obj.title);
	component.show();
	return modal;
};

function groww(title, text, type, icon, timer){
	if(title =='') title = "Alerta:";
	var template = $('<div class="alert alert-'+type+'" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button><i class="fa fa-'+icon+' fa-lg"></i> <strong>'+title+'</strong><br/> '+text+'</div>');
	$(".panel-body").prepend(template);
	template.hide().slideDown();
	
	if(timer!=null){
		setTimeout(function(){
			template.slideUp('slow'); 
		}, timer);
	}
    $(document).find("html, body").animate({ scrollTop: 0 }, "slow");
}

